package com.curry.android.app;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;

import com.curry.android.R;
import com.curry.android.util.ConfigHelper;
import com.curry.android.util.StrHelper;


public class AppContext extends Application {
	
	//刚启动程序
	public static boolean isStart = true;

	//程序上下文
	public static AppContext context;
		
	public static ExecutorService threadPool = Executors.newCachedThreadPool();
	
	//机子唯一标识
	private static String deviceUUID = null;
	
	//机子唯一标识SHA散列
	private static String deviceUUIDSHA = null;
	
	private static String myUrl=null;
	
	private static String myUrlModl="http://URL/wsq/app/v1/estate/";
	
	private static String url_path="";
	
	public static TaskManager taskManager = new TaskManager();
	
	private static String accesToken=null;
	
	

	/**
	 * 单例模式中获取唯一的AppContext实例
	 * @return
	 */
	public static AppContext getAppContext() {
		if (null == context) {
			context = new AppContext();
		}
		
		return context;
	}
	
	@Override
	public void onCreate() {
		super.onCreate();
	
		getAppContext();	
		init();
	}
	
	private void init(){
		
		initNetworkPath(AppContext.this);
	} 
	
	private static void initNetworkPath(Context ctx){
		SharedPreferences preferences = ConfigHelper.getSharedPreferences(
				ctx, "ip_path", ctx.MODE_PRIVATE);
		String network_path=preferences.getString("NETWORK_PATH", "");
		Log.i("network_path", "network_path"+network_path);
		if(network_path!=null&&!"".equals(network_path)){
			myUrl=myUrlModl.replaceAll("URL",network_path);
			url_path=network_path;
		}else{
			myUrl=myUrlModl.replaceAll("URL",ctx.getString( R.string.url));
			url_path=ctx.getString( R.string.url);
		}
	}
	
	public static void copyNetworkToUrl(String url){
		url_path=url;
		myUrl=myUrlModl.replaceAll("URL",url);
	
	}
	
	/**
	 * 获取手机型号
	 */
	public static String getPhoneModel(){
		return Build.MODEL;
	}	
	
	/**
	 * 获取机子唯一标识SHA散列
	 */
	public static String getDeviceUUIDSHA() {
		if (deviceUUIDSHA == null) {
			String uuid=AppContext.getDeviceUUID();
			if(TextUtils.isEmpty(uuid))
				deviceUUIDSHA="";
			else
				deviceUUIDSHA = StrHelper.getStringSHA(uuid) ;
		}
		return deviceUUIDSHA;
	}
	
	/**
	 * 获取机子唯一标识
	 */
	public static String getDeviceUUID() {
		if (deviceUUID == null) {
			TelephonyManager tm = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
			deviceUUID = tm.getDeviceId();
			if(TextUtils.isEmpty(deviceUUID))
				deviceUUID="";
		}
		
		return deviceUUID;
	}

	public static String getMyUrl() {
		Log.i("myUrl", "myUrl"+myUrl);
		return myUrl;
	}
	
	public static String getAccesToken() {
		return accesToken;
	}

	public static void setAccesToken(String accesToken) {
		AppContext.accesToken = accesToken;
	}
	public static String getUrl_path(){
		return url_path;
	}
}
